<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Repositories\OrderRepo;
use Cache;

class OrderController extends ApiBaseController
{
      //生产订单
    public function submit(Request $request)
    {
        $user_id = $this->getUserId();

        $address = $request->address;
        $products = $request->products;
        $beizhu   = $request->beizhu;
        $orderRepo = new OrderRepo();

        $orderRepo->createOrder($user_id,$address,$products,$beizhu);
        return $this->sucRes(200);
    }

    //生产订单
    public function create(Request $request)
    {
        $user_id = $this->getUserId();

        $address = $request->address;
        $products = $request->products;
        $beizhu   = $request->beizhu;
        $orderRepo = new OrderRepo();

        $orderRepo->createOrder($user_id,$address,$products,$beizhu);
        return $this->sucRes(200);
    }

    public function info(Request $request)
    {
        $order_id =  $request->order_id;
        $repo = new OrderRepo();
        $info = $repo->getOrderInfo($order_id);
        return $this->sucRes(200,$info);
    }
    //订单列表
    public function lists(Request $request)
    {
        $state = $request->state;
        $repo = new OrderRepo();
        $uid = $this->getUserId();
        $data = $repo->getOrderListsByUid($uid,['state'=>$state]);
        return $this->sucRes(200,$data);
    }
    //获取缓存
    public function setCache(Request $request)
    {
        $product = $request->input('products');
        if(!is_array($product)) {
           return $this->errRes(-1,'格式错误非array');
        }
        $user_id =$this->getUserId();
        $key = 'user:'.$user_id.':product';
        Cache::forget($key);
        Cache::put($key,$product,15*60);
        return $this->sucRes();

    }
    //获取缓存
    public function getCache()
    {
        $user_id =$this->getUserId();

        $key = 'user:'.$user_id.':product';

        $data = Cache::get($key);
        if(empty($data)) {
            $data = [];
        }
        return $this->sucRes(200,$data);
    }
    //提货
    public function take(Request $request)
    {
         $repo = new OrderRepo();
         $repo->takeProduct($request->id);

         return $this->sucRes();
    }
}
